package com.qf.service.Impl;


import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import com.qf.pojo.UserExample;
import com.qf.service.LoginUserService;
import com.qf.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoginUserServiceImpl implements LoginUserService {
    @Autowired
    private UserMapper userMapper;
    /*根据用户名查询用户信息*/
    @Override
    public User findUserByName(String username) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        //criteria.andPasswordEqualTo()

        List<User> users = userMapper.selectByExample(userExample);
        /*List<User> users = userMapper.findUserByName(username);
        */
        if(users!=null&&users.size()>0){
            return users.get(0);
        }
        return null;
    }
    //验证用户信息
    @Override
    public User loginYanZhen(String username, String password)throws Exception{
        User userByName = findUserByName(username);
        if(userByName==null){
            /*throw new Exception("用户名或密码错误");*/
            return null;
        }

        String md5ofStr = new MD5().getMD5ofStr(password);
        //String md5ofStr=userByName.getPassword();

        if(!userByName.getPassword().equals(md5ofStr)) {
            /*throw new Exception("用户名或密码错误");*/
            return null;
        }
        userByName.setPassword(null);

        return userByName;
    }
    //注册
    @Override
    public int zhuCe(User user) {

        String password = user.getPassword();
        String password2 = new MD5().getMD5ofStr(password);
        user.setPassword(password2);
        int insert = userMapper.insert(user);
        return insert;
    }
    //查询所有用户
    @Override
    public List<User> users() {
        List<User> users = userMapper.users();
        return users;
    }
    //修改
    @Override
    public void updateUser(User user) {

        userMapper.updateByPrimaryKey(user);

    }
    //id查询
    @Override
    public User updateUser2(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);

        return user;
    }
    //删除
    @Override
    public void getDelect(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }
}
